home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / tie / src / lib.c < prev    next >
C/C++ Source or Header  |  1991-10-18  |  2KB  |  95 lines

  1. /*
  2.  *  一般的な関数
  3.  
  4.  *  --------------  --  ---------------------------
  5.  *  91/04/14 03:22  01  get_fsize()
  6.  *  91/04/26 21:32  02  kstrlen()
  7.  *  91/05/27 18:28  03  strdup()
  8.  *  91/07/27 04:41  04  trim()
  9.  *  91/07/29 18:46  05  access()
  10. */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "sugi.h"
  17.  
  18. #define iskanji(c)   ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc))
  19. #define iskanji2(c)  ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc))
  20.  
  21.  
  22. /*  指定のファイル・ポインタの指すファイルの大きさを返す  */
  23.  
  24. long    get_fsize( FILE *fp )
  25. {
  26.     long    now, fsize ;
  27.  
  28.     now = ftell( fp ) ;
  29.  
  30.     fseek( fp, 0L, SEEK_END ) ;
  31.     fsize = ftell( fp ) ;
  32.  
  33.     fseek( fp, now, SEEK_SET ) ;
  34.  
  35.     return( fsize ) ;
  36. }
  37.  
  38.  
  39. /*  全角文字も1文字として数えるstrlen  */
  40.  
  41. int     kstrlen( u_char *str )
  42. {
  43.     u_char  *p ;
  44.     int     len = 0 ;
  45.  
  46.     for( p = str ; *p != '\0' ; p ++, len ++ )
  47.         if( iskanji( *p ) && iskanji2( *(p+1) ) )
  48.             p ++ ;
  49.  
  50.     return( len ) ;
  51. }
  52.  
  53.  
  54. /*  コピー先を確保し、文字列をコピーする  */
  55.  
  56. char    *strdup( char *str )
  57. {
  58.     REGS    char    *p ;
  59.  
  60.     if( ( p = (char *)malloc( strlen( str )+1 ) ) != NULL )
  61.         strcpy( p, str ) ;
  62.  
  63.     return p ;
  64. }
  65.  
  66.  
  67. /*  文字列の最後の無駄なスペースを削除する  */
  68.  
  69. u_char  *trim( u_char *str )
  70. {
  71.     REGS    int     i ;
  72.  
  73.     for( i = strlen( str ) ; -- i >= 0 ; str[i] = '\0' )
  74.         if( str[i] != ' ' )
  75.             break ;
  76.  
  77.     return str ;
  78. }
  79.  
  80. /*  ファイルへのアクセスを検査する  */
  81.  
  82. int     access( char *path, char *mode )
  83. {
  84.     FILE    *fp ;
  85.  
  86.     if( ( fp = fopen( path, mode ) ) == NULL )
  87.         return ERROR ;
  88.     else
  89.         fclose( fp ) ;
  90.  
  91.     return ERROR+1 ;
  92. }
  93.  
  94.  
  95.